layui.use(['jquery','carousel','layer'], function(){
  	var carousel = layui.carousel,$ = layui.jquery,layer = layui.layer;
  	var elemBanner = $('#FLY-extend-banner');
  	
  	carousel.render({
	    elem: elemBanner
	    ,width: '100%' //设置容器宽度
	    ,height: elemBanner.data('height')
	    ,arrow: 'none' //始终显示箭头
	    ,anim: 'fade' //切换动画方式
	    ,interval: 5000
  	});
  	$("input[name = 'kws']").bind('input propertychange',function(){
  		var val = $(this).val()
  		createScript(val);
  	})
  	$("body").on('click',function(e){
  		var ul = $('#layui-ul');
  		ul.empty()
  		ul.css({
  			'display': 'none'
  		})
  	})
  	$("input[name = 'kws']").focus(function(){
  		$(this).trigger('propertychange');
  	})
  	$('#searchBtn').on('click',function(){
  		var url = $("input[name = 'kws']").val();
  		if(!url){
  			return layer.msg('请先输入', function(){});
  		}
  		var a = document.createElement('a');
  		a.href = 'search.html#'+url
  		a.target = '_blank';
  		a.click();
	  	/*layer.full(layer.open({
		  	type: 2,
		  	title: '视频',
		  	content: ['play.html#' + url, 'no' ]
		}))*/
  	});
  	function createLi(item){
  		return ['<li class="layui-li" data-id="',item.id,'">','<a href="play.html#https://v.qq.com/x/cover/',item.id,'.html" target="_blank">',item.title, '</a><span>',item.ar,'</span>','</li>'].join('');
  	}
  	function createScript(name){
  		var script = document.createElement('script');
  		script.src = 'https://s.video.qq.com/smartbox?callback=searchFun&plat=2&ver=0&num=10&otype=json&query='+ name +'&uid=acc74aac-9a3e-46ee-b29b-f265ef5bd98a&_=1558090848806'
  		document.body.appendChild(script);
  		function onScriptLoad(e){
  			document.body.removeChild(script);
  		}
  		if(script.attachEvent && !(script.attachEvent.toString && script.attachEvent.toString().indexOf('[native code') < 0)){
	        script.attachEvent('onreadystatechange', function(e){
	          onScriptLoad(e);
	        });
	      } else {
	        script.addEventListener('load', function(e){
	          onScriptLoad(e);
	        }, false);
	      }
  	}
  	function appendBodyLi(appendLi){
  		if(appendLi.length === 0){
  			return ;
  		}
  		var ul = $('#layui-ul');
  		ul.empty();
  		ul.append(appendLi.join(''));
  		var setting = {
  			'overflow-y': '',
  			'height': '',
  			'display': 'none'
  		};
  		//先将基本样式去掉
  		ul.css(setting)
  		if(appendLi.length > 5){
  			setting['overflow-y'] = 'auto';
  		}
  		if(appendLi.length < 5){
  			var len = appendLi.length,
  			cssHeight = 56 * len;
  			setting['height'] = cssHeight + 'px';
  		}
  		ul.css(setting)
  		ul.show();
  	}
  	window.searchFun = function searchFun(res){
  		var result = res.result;
  		if(result.code === 0){
  			var items = res.item;
  			var appendLi = [];
  			for (var i = 0; i < items.length; i++) {
  				var item = items[i];
  				if(item.url && item.tt){
  					appendLi.push(createLi(item))
  				}
  			}
  			appendBodyLi(appendLi);
  		}
	}
});